package com.game.logic.netty;

import com.game.framework.message.protobuf.BaseProto;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;


@Component
public class ClientNettyInitializer extends ChannelInitializer<SocketChannel> {
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    protected void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast("encoder", new ProtobufEncoder());
        pipeline.addLast("decoder", new ProtobufDecoder(BaseProto.BaseReq.getDefaultInstance()));
        pipeline.addLast(new ClientNettyHandler(applicationContext));
    }
}